//
//  WSRegistView.m
//  GroupFourFM
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 GroupFour. All rights reserved.
//

#import "WSRegistView.h"

@interface WSRegistView ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *userName;

@property (nonatomic, strong) UITextField *passWord;

@property (nonatomic, strong) UITextField *passWordT;

@property (nonatomic, strong) UITextField *iphone;

@property (nonatomic, strong) UIButton *but;

@end

@implementation WSRegistView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
        self.backgroundColor = [UIColor colorWithWhite:0.957 alpha:1.000];
    }
    return self;
}

- (void)createSubView {

#pragma mark 返回 1000
    UIButton *returnBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnBut setImage:[UIImage imageNamed:@"iconfont-addcollapse"] forState:UIControlStateNormal];
    [returnBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:returnBut];
    [returnBut mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@20);
        make.top.mas_equalTo(@30);
        make.height.width.mas_equalTo(@32);
    }];
    
#pragma mark 输入框 1010 1011
    UIView *textView = [[UIView alloc] init];
    textView.backgroundColor = [UIColor whiteColor];
    [textView.layer setBorderWidth:1.5];
    [textView.layer setBorderColor:[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1].CGColor];
    [self addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@-1);
        make.top.mas_equalTo(@150);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(@(45*4));
    }];
    
    
    _userName = [[UITextField alloc] init];
    [textView addSubview:_userName];
    _userName.placeholder = @"手机/邮箱";
    [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@75);
        make.top.mas_equalTo(@0);
        make.right.mas_equalTo(@20);
        make.height.mas_equalTo(@45);
    }];
    //
    UIView *bview = [[UIView alloc] init];
    bview.backgroundColor = [UIColor colorWithWhite:0.851 alpha:1.000];
    [textView addSubview:bview];
    [bview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@15);
        make.top.equalTo(self.userName.mas_bottom);
        make.right.mas_equalTo(@-5);
        make.height.mas_equalTo(@1);
    }];
    self.passWord = [UITextField new];
    [textView addSubview:_passWord];
    _passWord.placeholder = @"密码";
    [self.passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.height.equalTo(self.userName);
        make.top.equalTo(self.userName.mas_bottom);
    }];
    UIView *cview = [[UIView alloc] init];
    cview.backgroundColor = [UIColor colorWithWhite:0.851 alpha:1.000];
    [textView addSubview:cview];
    [cview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@15);
        make.top.equalTo(self.passWord.mas_bottom);
        make.right.mas_equalTo(@-5);
        make.height.mas_equalTo(@1);
    }];

    self.passWordT = [UITextField new];
    [textView addSubview:_passWordT];
    _passWordT.placeholder = @"确认密码";
    [self.passWordT mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.height.equalTo(self.userName);
        make.top.equalTo(self.passWord.mas_bottom);
    }];
    UIView *dview = [[UIView alloc] init];
    dview.backgroundColor = [UIColor colorWithWhite:0.851 alpha:1.000];
    [textView addSubview:dview];
    [dview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@15);
        make.top.equalTo(self.passWordT.mas_bottom);
        make.right.mas_equalTo(@-5);
        make.height.mas_equalTo(@1);
    }];



    self.iphone = [UITextField new];
    [textView addSubview:_iphone];
    _iphone.placeholder = @"手机号";
    [self.iphone mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.height.equalTo(self.userName);
        make.top.equalTo(self.passWordT.mas_bottom);
    }];
    _iphone.keyboardType = UIKeyboardTypeNumberPad;
    [_iphone addTarget:self action:@selector(textFieldAction:) forControlEvents:UIControlEventEditingChanged];

    UILabel *label = [[UILabel alloc] init];
    [textView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@0);
        make.bottom.equalTo(self.iphone.mas_bottom);
        make.height.mas_equalTo(@45);
        make.width.mas_equalTo(@75);
    }];
    //label.text = @"+86";
    label.textAlignment = NSTextAlignmentCenter;
    
    
#pragma mark 登录键 1003
    self.but = [UIButton buttonWithType:UIButtonTypeCustom];
    self.but.tag = 1003;
    [self.but.layer setCornerRadius:5];
    [self.but setTitle:@"注册" forState:UIControlStateNormal];
    [self.but addTarget:self action:@selector(registAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_but];
    [self.but mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self);
        make.top.equalTo(textView.mas_bottom).offset(20);
        make.height.mas_equalTo(@40);
        make.width.equalTo(self).multipliedBy(0.5);
    }];
    self.but.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.79 alpha:1];
    [self.but setUserInteractionEnabled:NO];
    
}

- (void)buttonAction:(UIButton *)button {

    [self.delegate backViewController];
}

- (void)textFieldAction:(UITextField *)textField {

    if (textField.text.length == 11) {
        self.but.backgroundColor = [UIColor colorWithRed:0.082 green:0.565 blue:0.847 alpha:1.000];
        [self.but setUserInteractionEnabled:YES];
    }
    else {
    
        self.but.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.79 alpha:1];
        [self.but setUserInteractionEnabled:NO];
    }
}

- (void)registAction:(UIButton *)button {
    
    [self.delegate registWithPassWord:_passWord.text repassWord:_passWordT.text iphone:_iphone.text userName:_userName.text];
}

@end
